<?php
/**
 * @see pdMarkdownException
 */
require_once 'pdMarkdownException.php';

/**
 * @see Markdown_Parser
 */
require_once sfConfig::get('pd_markdown_parser_lib');


/**
 * pdMarkdown is the core class of pdMarkdownPlugin.
 *
 * It's a bridge between Symfony class standards and MarkdownExtra_Parser,
 * an excellent PHP parser for Markdown documents.
 *
 * PHP Markdown_Parser and MarkdownExtra_Parser are developed by Michel Fortin.
 * You can get a copy at http://www.michelf.com/projects/php-markdown/
 * along with examples and full documentation.
 *
 * @package         pdMarkdownPlugin
 * @subpackage      helper
 * @author          Nicolas Surian aka NairuS <contact@nairus.fr>
 */
class pdMarkdown extends MarkdownExtra_Parser
{
    /**
     * Class constructor
     *
     * @return  void
     */
    public function __construct()
    {
        $class = sfConfig::get('pd_markdown_parser', 'MardownExtra_Parser');
        if (!class_exists($class)) {
            throw new pdMarkdownException("Parser class '$class' doens't exists");
        }

        // call parent constructor
        parent::$class();
    }

    /**
     * Class destructor
     *
     * @return  void
     */
    public function __destruct()
    {
    }

    /**
     * Convert text to HTML
     *
     * This function converts given markdown text to HTML.
     *
     * @param   string  $text
     * @return  string
     */
    public function convert($text)
    {
        // parse, convert and return
        return $this->transform($text);
    }

    /**
     * Convert file to HTML
     *
     * This function converts the content of given file to HTML.
     * The file, obviously, should contain only Markdown text.
     *
     * @param   string  $file
     * @return  string
     * @throws  pdMarkdownException if $file isn't readable
     */
    public function convertFile($file)
    {
        // check whether file is readable
        if (!is_readable($file)) {
            throw new pdMarkdownException("Unable to read file '$file'");
        }

        $text = file_get_contents($file);

        // convert
        return $this->convert($text);
    }

    /**
     * Convert text to HTML
     *
     * This function converts given markdown text to HTML.
     * It's just a convenient static shortcut for pdMarkdown::convert()
     *
     * @param   string  $text
     * @return  string
     * @see     pdMarkdown::convert()
     * @static
     */
    public static function doConvert($text)
    {
        $parser = self::getParserInstance();

        // parse, convert and return
        return $parser->convert($text);
    }

    /**
     * Convert file to HTML
     *
     * This function converts the content of given file to HTML.
     * The file, obviously, should contain only Markdown text.
     * It's just a convenient static shortcut for pdMarkdown::convertFile()
     *
     * @param   string  $file
     * @return  string
     * @throws  pdMarkdownException if $file isn't readable
     * @see     pdMarkdown::convertFile()
     * @static
     */
    public static function doConvertFile($file)
    {
        $parser = self::getInstance();

        // convert
        return $parser->convertFile($file);
    }

    /**
     * Returns a Markdown Parser instance
     *
     * @return  mixed   Markdown parser instance
     */
    public static function getInstance()
    {
        static $parser;
        static $class = __CLASS__;

        // get parser instance
        if (!($parser instanceof $class)) {
            $parser = new $class;
        }

        return $parser;
    }
}


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * c-hanging-comment-ender-p: nil
 * End:
 */
